package com.boss.salary.controller;

import com.alibaba.fastjson.JSONObject;
import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.service.ICommonService;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.SalaryPaymentTicket;
import com.boss.salary.service.ISalaryPaymentTicketService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
    @RestController
    @RequestMapping(value = "/SalaryPaymentTicketController", produces = MediaType.APPLICATION_JSON)
public class SalaryPaymentTicketController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(SalaryPaymentTicketController.class);

    @Autowired
    private ISalaryPaymentTicketService salaryPaymentTicketService;
        @Autowired
        private ICommonService iCommonService;
    /**
     * 分页查询
     * @param
     * @param limit
     * @return
     */
    @GetMapping("/queryPaymentTicketPage")
    public ApiResultDTO queryPaymentTicketPage(
                                               @RequestHeader("tokenid") String tokenid,
                                               @RequestParam(value = "page") String page,
                                               @RequestParam(value = "limit") String limit,
                                               @RequestParam(value = "agencyId") String agencyId,
                                               @RequestParam(value = "flag") String flag,
                                               @RequestParam(value = "agencyName") String agencyName,
                                               @RequestParam(value = "payeeAcctNo") String payeeAcctNo,
                                               @RequestParam(value = "payAppAmt") String payAppAmt,
                                               @RequestParam(value = "payeeAcctName") String payeeAcctName,
                                               @RequestParam(value = "payeeAcctBankName") String payeeAcctBankName){

        try {
            Map<String, Object> parameters = new HashMap<>(8);
            UserDTO userDTO = getUser(tokenid);
            List<String> childrenId = iCommonService.getAgencyChildrenId(userDTO, agencyId);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<SalaryPaymentTicket> paymentTicketPageDTO = salaryPaymentTicketService.queryPaymentTicketPage(paginationDTO,userDTO.getProvince(), agencyId,flag, agencyName, payeeAcctNo, payAppAmt, payeeAcctName, payeeAcctBankName);
            return ApiResultDTO.success(paymentTicketPageDTO,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }

    }

        /**
         * 修改
         *
         * @param salaryPaymentTicket
         * @return
         */
        @PostMapping("/UpdatePaymentTicket")
        public ApiResultDTO updatePaymentTicket(@RequestHeader("tokenid") String tokenid,
                                                             @RequestBody SalaryPaymentTicket salaryPaymentTicket) {
            try {
                UserDTO userDTO = getUser(tokenid);
                salaryPaymentTicketService.updatePaymentTicket(salaryPaymentTicket);
                return ApiResultDTO.success(null, "保存成功");
            } catch (Exception e) {
                logger.error(" 单位信息表修改异常", e);
                return ApiResultDTO.error(ERROR_RSCODE, "单位信息表修改异常");
            }
        }

        /**
         * 批量插入
         *
         * @param salaryPaymentTickets
         * @return
         */
        @PostMapping("/InsertPaymentTicket")
        public ApiResultDTO insertPaymentTicket(@RequestHeader("tokenid") String tokenid,
                                                @RequestBody List<SalaryPaymentTicket> salaryPaymentTickets) {
            try {
                UserDTO userDTO = getUser(tokenid);
                Map<String,Object> map= salaryPaymentTicketService.insertPaymentTicket(salaryPaymentTickets,userDTO);
                JSONObject json = new JSONObject(map);
                return ApiResultDTO.success(json, "存入成功");
            } catch (Exception e) {
                logger.error(" 存入失败", e);
                return ApiResultDTO.error(ERROR_RSCODE, e.getMessage()+"存入失败");
            }
        }
        /**
         * 推送
         *
         * @param salaryPaymentTicketMap
         * @return
         */
        @PostMapping("/modifyIsPushAgain")
        public ApiResultDTO modifyIsPushAgain(@RequestHeader("tokenid") String tokenid,
                                              @RequestBody List<Map<String,Object>> salaryPaymentTicketMap) {
            try {
                UserDTO userDTO = getUser(tokenid);
                    salaryPaymentTicketService.modifyIsPushAgain(salaryPaymentTicketMap,userDTO,tokenid);
                return ApiResultDTO.success(null, "推送成功");
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                return ApiResultDTO.error(ERROR_RSCODE, e.getMessage()+"推送失败");
            }
        }
}
